package com.wondersgroup.fjzlpt.console.dao.impl.bqxx;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.console.dao.inter.bqxx.BqxxDAO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lion on 2017/8/14.
 */
@Repository
public class BqxxDAOImpl extends BaseJTDAOImpl implements BqxxDAO {

    @Autowired
    @Qualifier("TB_ZD_BQXX")
    @Override
    public void setTb(TableBean tb) {
        super.setTb(tb);
    }


    public JSONObject queryPageBqxx(Map map){
        StringBuilder sbud = new StringBuilder("select a.*, b.yljgmc, c.ksmc \n")
                .append("from tb_zd_bqxx a, tb_zd_yljgxx b, tb_zd_ksxx c where 1=1  \n")
                .append("and a.ptjgid=b.ptjgid and b.ptjgid=c.ptjgid and c.jb='1' and a.ksdm=c.ksdm \n")
                .append("");
        List list = new ArrayList();
        String value = (String)map.get("PTJGID");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and b.PTJGID=? \n");
            list.add(value);
        }else{
            value = (String)map.get("YLJGMC");
            if(!StringUtils.isEmpty(value)){
                sbud.append(" and b.YLJGMC like ? \n");
                list.add("%"+value+"%");
            }
        }
        value = (String)map.get("KSDM");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and a.KSDM = ? \n");
            list.add(value);
        }
        value = (String)map.get("BQMC");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and a.BQMC like ? \n");
            list.add("%"+value+"%");
        }
        value = (String)map.get("XZQHDM");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and b.XZQHDM like ? \n");
            list.add(value+"%");
        }
        PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sbud.toString(), "order by a.PTJGID, a.KSDM, a.BQDM", list.toArray());
        return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
    }
}
